package com.hinnotek.warehouse.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.hinnotek.warehouse.api.WarehouseApi
import com.hinnotek.warehouse.utils.StringUtils
import com.lee.common.network.getRequestBody
import com.lee.common.utils.PAGE_SIZE
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 * 二维码扫描
 */
@HiltViewModel
class ScanAddModel @Inject constructor() :
    BaseViewModel() {

    var wareId = 0
    var applyDeviceId = 0
    var productNum = ""


    val loadDetail = MutableLiveData<Boolean>()
    val loading1 = MutableLiveData<Boolean>()
    val loading2 = MutableLiveData<Boolean>()
    val loading3 = MutableLiveData<Boolean>()
    val loading4 = MutableLiveData<Boolean>()

    fun loadData1() {
        loading1.value = true
        loading.value = true
    }

    fun loadData2() {
        loading2.value = true
        loading.value = true
    }

    fun loadData3() {
        loading3.value = true
        loading.value = true
    }

    fun loadData4() {
        loading4.value = true
        loading.value = true
    }

    fun loadDeviceDetail() {
        loadDetail.value = true
        loading.value = true
    }

    //设备详情
    private val deviceDetail = Transformations.switchMap(loadDetail) {
        api.getDeviceAccountByProductNum(productNum)
    }

    //入库
    private val stockIn = Transformations.switchMap(loading1) {
        api.addInWarehourseApplyFormDeviceDetail(getRepBody(wareId, applyDeviceId, productNum))
    }

    //出库
    private val stockOutOf = Transformations.switchMap(loading2) {
        api.addOutWarehourseApplyFormDeviceDetail(getRepBody(wareId, applyDeviceId, productNum))
    }

    //借用
    private val borrowApply = Transformations.switchMap(loading3) {
        api.addBorrowWarehourseApplyFormDeviceDetail(getRepBody(wareId, applyDeviceId, productNum))
    }

    //报损
    private val lossReport = Transformations.switchMap(loading4) {
        api.addFrmlossWarehourseApplyFormDeviceDetail(getRepBody(wareId, applyDeviceId, productNum))
    }

    private fun getRepBody(wareId: Int, applyDeviceId: Int, productNum: String) =
        getRequestBody(mutableMapOf<String, Any>().apply {
            put("wareId", wareId)
            put("applyDeviceId", applyDeviceId)
            put("productNum", productNum)

        })

    //入库扫描
    val stockInData = mapData(stockIn)

    //出库扫描
    val stockOutOfData = mapData(stockOutOf)

    //借用扫描
    val borrowApplyData = mapData(borrowApply)

    //报损扫描
    val lossReportData = mapData(lossReport)

    val deviceDetailData = mapData(deviceDetail)

}